import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:x3_base_core/base_utils/base_transform_util.dart';
import 'package:x3_base_core/base_utils/base_utils.dart';

class BaseSelectHoursAndMinutes extends StatefulWidget {
  final int hours;
  final int minutes;
  final Function(int hours, int minutes) selected;

  const BaseSelectHoursAndMinutes({Key key, @required this.selected, this.hours, this.minutes}) : super(key: key);

  @override
  State<BaseSelectHoursAndMinutes> createState() => _BaseSelectHoursAndMinutesState();
}

class _BaseSelectHoursAndMinutesState extends State<BaseSelectHoursAndMinutes> {
  static final double _itemHeight = 48;

  //小时数组
  List<int> _hoursList = [];

  //分钟数组
  List<int> _minutesList = [];

  //小时的索引
  int hoursIndex;

  //分钟的索引
  int minutesIndex;

  FixedExtentScrollController hoursScrollController;
  FixedExtentScrollController minutesScrollController;

  @override
  void initState() {
    super.initState();
    _setupData();
    _initSelectedIndex();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        double _columnWidth = constraints.maxWidth / 2;
        Size label = BaseUtils.boundingTextSize(context, '时', TextStyle(color: Colors.grey, fontSize: 16));
        return Stack(
          children: [
            Row(
              children: <Widget>[
                Expanded(child: _hoursPickerView()),
                Expanded(child: _minutesPickerView()),
              ],
            ),
            IgnorePointer(child: Container(color: Color(0xFFD1D1D6), margin: EdgeInsets.only(top: constraints.maxHeight / 2 - _itemHeight / 2), height: 1)),
            IgnorePointer(child: Container(color: Color(0xFFD1D1D6), margin: EdgeInsets.only(top: constraints.maxHeight / 2 + _itemHeight / 2), height: 1)),
            IgnorePointer(
              child: Container(
                margin: EdgeInsets.only(top: constraints.maxHeight / 2 - _itemHeight / 2 + label.height / 2),
                child: Row(
                  children: [
                    Expanded(
                      child: Padding(
                        padding: EdgeInsets.only(left: _columnWidth - 50),
                        child: Text('时', style: TextStyle(color: Colors.grey, fontSize: 16), maxLines: 1),
                      ),
                    ),
                    Expanded(
                      child: Padding(
                        padding: EdgeInsets.only(left: _columnWidth - 60),
                        child: Text('分', style: TextStyle(color: Colors.grey, fontSize: 16), maxLines: 1),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  Widget _hoursPickerView() {
    return Container(
      child: CupertinoPicker(
        scrollController: hoursScrollController,
        children: _buildHoursWidget(),
        looping: true,
        selectionOverlay: Center(),
        onSelectedItemChanged: (index) {
          setState(() {
            hoursIndex = index;
          });
          widget.selected(_hoursList[hoursIndex], _minutesList[minutesIndex]);
        },
        itemExtent: _itemHeight,
      ),
    );
  }

  ///年Widget
  List<Widget> _buildHoursWidget() {
    List<Widget> hoursListWidget = []; //先建一个数组用于存放循环生成的widget
    for (var item in _hoursList) {
      hoursListWidget.add(Center(child: Text('${BaseTransformUtil.format(item)}', style: TextStyle(color: Colors.black87, fontSize: 16), maxLines: 1)));
    }
    return hoursListWidget;
  }

  Widget _minutesPickerView() {
    return Container(
      child: CupertinoPicker(
        scrollController: minutesScrollController,
        children: _buildMinutesWidget(),
        looping: true,
        selectionOverlay: Center(),
        onSelectedItemChanged: (index) {
          setState(() {
            minutesIndex = index;
          });
          widget.selected(_hoursList[hoursIndex], _minutesList[minutesIndex]);
        },
        itemExtent: 44,
      ),
    );
  }

  List<Widget> _buildMinutesWidget() {
    List<Widget> minutesListWidget = []; //先建一个数组用于存放循环生成的widget
    for (var item in _minutesList) {
      minutesListWidget.add(Center(child: Text("${BaseTransformUtil.format(item)}", style: TextStyle(color: Colors.black87, fontSize: 16), maxLines: 1)));
    }

    return minutesListWidget;
  }

  ///初始化数据
  void _setupData() {
    for (int i = 0; i <= 14; i++) {
      _hoursList.add(8 + i);
    }

    for (int i = 0; i < 60; i++) {
      _minutesList.add(i);
    }
  }

  ///初始化时间索引
  void _initSelectedIndex() {
    hoursIndex = 0;
    if (widget.hours != null) {
      int _index = _hoursList.indexWhere((element) => element == widget.hours);
      if (_index != -1) hoursIndex = _index;
    }
    hoursScrollController = FixedExtentScrollController(initialItem: hoursIndex);
    minutesIndex = 0;
    if (widget.hours != null) {
      int _index = _minutesList.indexWhere((element) => element == widget.minutes);
      if (_index != -1) minutesIndex = _index;
    }
    minutesScrollController = FixedExtentScrollController(initialItem: minutesIndex);
  }
}
